﻿using Microsoft.AspNetCore.Mvc;
using RBAC_Application.Dto.Output;
using RBAC_Application.Repository.Interface;
using RBAC_Application.Service.RBAC;
using Standard;

namespace B.MES.Login.Controllers
{
    /// <summary>
    /// 角色权限管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class RolePermissionController : ControllerBase
    {
        private readonly IRbacService _rbacService;

        public RolePermissionController(IRbacService rbacService)
        {
            _rbacService = rbacService;
        }

        /// <summary>
        /// 给角色分配权限
        /// </summary>
        [HttpPost("assign")]
        public ApiResult<int> AssignRolePermission([FromQuery] long roleId, [FromQuery] long permissionId)
        {
            try
            {
                _rbacService.AssignRolePermission(roleId, permissionId);
                return new ApiResult<int>
                {
                    Code = ApiCode.Success,
                    ResultData = 1,
                    Message = "分配成功"
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<int>
                {
                    Code = ApiCode.InternalError,
                    Message = ex.Message
                };
            }
        }

        /// <summary>
        /// 移除角色的权限
        /// </summary>
        [HttpPost("remove")]
        public ApiResult<int> RemoveRolePermission([FromQuery] long roleId, [FromQuery] long permissionId)
        {
            try
            {
                _rbacService.RemoveRolePermission(roleId, permissionId);
                return new ApiResult<int>
                {
                    Code = ApiCode.Success,
                    ResultData = 1,
                    Message = "移除成功"
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<int>
                {
                    Code = ApiCode.InternalError,
                    Message = ex.Message
                };
            }
        }

        /// <summary>
        /// 查询角色拥有的权限
        /// </summary>
        [HttpGet("role-permissions/{roleId}")]
        public ApiResult<List<PermissionDto>> GetRolePermissions(long roleId)
        {
            try
            {
                var permissions = _rbacService.GetRolePermissions(roleId);
                return new ApiResult<List<PermissionDto>>
                {
                    Code = ApiCode.Success,
                    ResultData = permissions,
                    Message = "获取成功"
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<List<PermissionDto>>
                {
                    Code = ApiCode.InternalError,
                    Message = ex.Message
                };
            }
        }

        /// <summary>
        /// 查询权限下的角色
        /// </summary>
        [HttpGet("permission-roles/{permissionId}")]
        public ApiResult<List<RoleDto>> GetPermissionRoles(long permissionId)
        {
            try
            {
                var roles = _rbacService.GetPermissionRoles(permissionId);
                return new ApiResult<List<RoleDto>>
                {
                    Code = ApiCode.Success,
                    ResultData = roles,
                    Message = "获取成功"
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<List<RoleDto>>
                {
                    Code = ApiCode.InternalError,
                    Message = ex.Message
                };
            }
        }
    }
}